package com.ptadmin.report.mws;

import java.time.LocalDate;
import java.util.Arrays;

import javax.xml.datatype.XMLGregorianCalendar;

import com.amazonaws.mws.MarketplaceWebServiceException;
import com.amazonaws.mws.model.IdList;
import com.amazonaws.mws.model.ReportRequestInfo;
import com.amazonaws.mws.model.RequestReportRequest;
import com.amazonaws.mws.model.RequestReportResponse;
import com.amazonaws.mws.model.RequestReportResult;
import com.amazonaws.mws.model.ResponseMetadata;
import com.amazonaws.mws.model.TypeList;
import com.ptadmin.report.InputParameter;

public class RequestReportWrapper extends ReportWrapperBase {

	private RequestReportRequest request;
	private RequestReportResponse response;

	public void buildRequest(String merchantId, String mwsAuthToken, IdList marketplaceIdList, String reportType,
			LocalDate startDate, LocalDate endDate) {
		this.request = new RequestReportRequest().withMerchant(merchantId).withMWSAuthToken(mwsAuthToken)
				.withMarketplaceIdList(marketplaceIdList).withReportType(reportType)
				.withReportOptions("ShowSalesChannel=true");

		XMLGregorianCalendar startDateCal = ReportWrapperBase.buildMwsDate(startDate);
		request.setStartDate(startDateCal);

		XMLGregorianCalendar endDateCal = ReportWrapperBase.buildMwsDate(endDate);
		request.setEndDate(endDateCal);
	}

	public void invokeRequest() {
		try {
			this.response = this.service.requestReport(this.request);
		} catch (MarketplaceWebServiceException ex) {
			System.out.println("Caught Exception: " + ex.getMessage());
			System.out.println("Response Status Code: " + ex.getStatusCode());
			System.out.println("Error Code: " + ex.getErrorCode());
			System.out.println("Error Type: " + ex.getErrorType());
			System.out.println("Request ID: " + ex.getRequestId());
			System.out.print("XML: " + ex.getXML());
			System.out.println("ResponseHeaderMetadata: " + ex.getResponseHeaderMetadata());
		} catch (Exception ex) {
			System.out.print(ex.toString());
		}
	}

	public String getReportRequestId() {
		return this.response.getRequestReportResult().getReportRequestInfo().getReportRequestId();
	}

	public void printResponse() {
		System.out.println("RequestReport Action Response");
		System.out.println("=============================================================================");
		System.out.println();

		System.out.print("    RequestReportResponse");
		System.out.println();

		if (response.isSetRequestReportResult()) {
			System.out.print("        RequestReportResult");
			System.out.println();

			RequestReportResult requestReportResult = response.getRequestReportResult();
			if (requestReportResult.isSetReportRequestInfo()) {
				System.out.print("            ReportRequestInfo");
				System.out.println();
				ReportRequestInfo reportRequestInfo = requestReportResult.getReportRequestInfo();
				if (reportRequestInfo.isSetReportRequestId()) {
					System.out.print("                ReportRequestId");
					System.out.println();
					System.out.print("                    " + reportRequestInfo.getReportRequestId());
					System.out.println();
				}
				if (reportRequestInfo.isSetReportType()) {
					System.out.print("                ReportType");
					System.out.println();
					System.out.print("                    " + reportRequestInfo.getReportType());
					System.out.println();
				}
				if (reportRequestInfo.isSetStartDate()) {
					System.out.print("                StartDate");
					System.out.println();
					System.out.print("                    " + reportRequestInfo.getStartDate());
					System.out.println();
				}
				if (reportRequestInfo.isSetEndDate()) {
					System.out.print("                EndDate");
					System.out.println();
					System.out.print("                    " + reportRequestInfo.getEndDate());
					System.out.println();
				}
				if (reportRequestInfo.isSetSubmittedDate()) {
					System.out.print("                SubmittedDate");
					System.out.println();
					System.out.print("                    " + reportRequestInfo.getSubmittedDate());
					System.out.println();
				}
				if (reportRequestInfo.isSetReportProcessingStatus()) {
					System.out.print("                ReportProcessingStatus");
					System.out.println();
					System.out.print("                    " + reportRequestInfo.getReportProcessingStatus());
					System.out.println();
				}
			}
		}
		if (response.isSetResponseMetadata()) {
			System.out.print("        ResponseMetadata");
			System.out.println();
			ResponseMetadata responseMetadata = response.getResponseMetadata();
			if (responseMetadata.isSetRequestId()) {
				System.out.print("            RequestId");
				System.out.println();
				System.out.print("                " + responseMetadata.getRequestId());
				System.out.println();
			}
		}

		System.out.println();
		System.out.println(response.getResponseHeaderMetadata());
		System.out.println();
	}
}
